package com.baizhi.wd.action;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baizhi.wd.entity.Person;
import com.baizhi.wd.service.TelbookService;
import com.baizhi.wd.service.TelbookServiceImpl;

public class AddPersonAction extends HttpServlet {
	public void service(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
		try{
			//1获取数据
			request.setCharacterEncoding("utf-8");
			
			String name = request.getParameter("name");
			String phone = request.getParameter("mobile");
			String telphone = request.getParameter("telphone");
			String emile = request.getParameter("email");
			String city = request.getParameter("city");
			String birthday = request.getParameter("birthday");
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Date utilbirthday = sdf.parse(birthday);
			
			Person person = new Person(null,name,phone,telphone,emile,city,utilbirthday);
			//2 调业务类的方法，添加一个
			TelbookService tb = new TelbookServiceImpl();
			
			tb.regist(person);
			// request中存命名属性
			
			// 3 根据结果，跳转--重定向/per/queryAllAction
			response.sendRedirect("/telbook_2/per/queryAllAction");
			/*RequestDispatcher rd = request.getRequestDispatcher("/per/addPersonView");
			rd.forward(request, response);*/
			}catch(Exception e){
				RequestDispatcher rd = request.getRequestDispatcher("/addPerson.html");
				rd.forward(request, response);
				e.printStackTrace();
				}
		
	}
}
